package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0090.java
 * @Author : WangRuoyu
 * @Date 2023/11/2 20:37
 */


public class Q0090 {
    int[] nums;
    boolean[] st;
    List<Integer> tmp = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();

    public void dfs(int k) {
        res.add(new ArrayList<>(tmp));
        if (k >= nums.length) {
            return;
        }
        for (int i = k; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1] && !st[i - 1]) {
                continue;
            }
            st[i] = true;
            tmp.add(nums[i]);
            dfs(i + 1);
            st[i] = false;
            tmp.remove(tmp.size() - 1);
        }
    }

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        this.nums = nums;
        this.st = new boolean[nums.length];
        java.util.Arrays.sort(nums);
        dfs(0);
        return res;
    }

    public static void main(String[] args) {
        Q0090 sl = new Q0090();
        int[] nums = {1, 2, 2, 2};
        System.out.println(sl.subsetsWithDup(nums));
    }
}